/**
 * Created by 云鹏 on 2014/9/11.
 */
var mongoose = require('../utils/MongooseConn.js');
var Schema = mongoose.Schema;
var settings = require('../config');
/**
 * 医师信息
 * */
var UserDoctor = new Schema({
    wcopenid : String,
    username : String,
    mobilephone : String,
    email : String,
    about : String,
    fordate : Date,
    state : {type: Number, default: 0}  //0未审核1已审核
});
UserDoctor.index({
    wcopenid : 1
});
var userDoctor = mongoose.model("UserDoctor", UserDoctor);
/*
 * 根据openid查询医师信息
 * */
userDoctor.prototype.findUserByOpenid = function (opneid, callback) {
    userDoctor.findOne({
        "wcopenid": opneid
    }, function (err, doc) {
        callback(err, doc);
    });
}

userDoctor.prototype.getCount = function (callback) {
    var query = userDoctor.find();
    query.count(function (err, count) {
        callback(err, count);
    })
}
userDoctor.prototype.updateUserDoctor = function (pid, callback) {
    userDoctor.update({
        _id: pid
    }, {
        $set: {
            state: 1
        }
    }, function (err, numberAffected) {
        console.log('-----------------------------------------', numberAffected);
        callback(err);
    })
}
userDoctor.prototype.findUserDoctor = function (skip, callback) {
    var query = userDoctor.find();
    query.skip((skip - 1) * settings.pre_page_row).limit(settings.pre_page_row);
    query.sort(
        {payoff_date: -1}
    )
    query.exec(function (err, result) {
        callback(err, result);
    })
}

userDoctor.prototype.findUserDoctorByid = function (uid, callback) {
    userDoctor.findOne({
        "_id": uid
    }, function (err, doc) {
        callback(err, doc);
    });
}

module.exports.UserDoctor = userDoctor;